<?php
/**
 * 库存增删记录表
 * String id
 * String sku
 * String title【标题】
 * String add_time【增删时间】
 * String add_user【操作人】
 * String isadd【是增加库存还是删除库存】
 */
class App_Model_StockDeladdLog extends App_Model_Abstract
{
	public function init()
	{
		parent::init();
		$this->table = App_Db_Table::factory('StockDeladdLog');
	}
	
	public function add($row)
	{
		return $this->table->insert($row);
	}
	
	public function listAll($sku,$isadd,$starttime,$endtime,$page=1,$limit=20)
	{
		$select = $this->table->select();
	    if(!empty($sku)){
			$select->where('sku = ?',$sku);
		}
		if(!($isadd=='')){
			$select->where('isadd = ?',$isadd);
		}
		if(!empty($starttime)){
			$select->where('add_time > ?',$starttime);
		}
		if(!empty($endtime)){
			$select->where('add_time < ?',$endtime);
		}
		$offset = ($page - 1) * $limit;
		$select->limit($limit,$offset);
	
		$select->order('add_time desc');
		return $this->table->fetchAll($select);
	}
	
	public function getTotalNum($sku,$isadd,$starttime,$endtime)
	{
		$where="where 1 ";
	
		if(!empty($sku)){
			$where.=" and sku='$sku'";
		}
		if(!($isadd=='')){
			$where.=" and isadd='$isadd'";
		}
		if(!empty($starttime)){
			$where.=" and add_time>='".$starttime."'";
		}
		if(!empty($endtime)){
			$where.=" and add_time<='".$endtime."'";
		}
		return $this->table->getAdapter()->fetchOne("select count(*) from stock_deladd_log ".$where);
	}
}